<template>
  <div class="articles-page">
    <!-- 上层区域 卡片 -->
    <el-card class="box-card">
      <!-- 存放面包屑 -->
      <div slot="header" class="clearfix">
        <my-bread>内容管理</my-bread>
      </div>
      <!-- 存放表单 -->
      <el-form ref="form" label-width="65px" size="small">
        <el-form-item label="状态：">
          <el-radio-group v-model="reqParams.status">
            <el-radio :label="null">全部</el-radio>
            <el-radio :label="0">草稿</el-radio>
            <el-radio :label="1">待审核</el-radio>
            <el-radio :label="2">审核通过</el-radio>
            <el-radio :label="3">审核失败</el-radio>
            <el-radio :label="4">已删除</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="频道：">
          <!-- 频道组件 -->
          <my-channel :value="reqParams.channel_id" @input="reqParams.channel_id=$event"></my-channel>

        </el-form-item>
        <!-- 日期格式化 value-format -->
        <el-form-item label="时间：">
          <!-- daterange 决定是不是要选两个日期 -->
          <el-date-picker
            @change="changeDate"
            value-format="yyyy-MM-dd"
            v-model="dataArr"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="">
          <el-button type="primary" @click="filterChannel()">查询</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 下层区域 -->
    <el-card class="box-card" style="margin-top: 20px">
      <div slot="header" class="clearfix">
        <span>根据筛选条件共查询到 {{ total }} 结果</span>
      </div>
      <!-- 表格 -->
       <el-table
        :data="articleTable"
        >
        <el-table-column label="封面">
          <!-- scope 里面有 row代表每一项 -->
          <template slot-scope="scope">
            <!-- element-ui 的图片组件可以显示无图片的状态 -->
            <el-image :src="scope.row.cover.images[0]" style="width:200px; height:150px">
              <!-- 错误显示的插槽 -->
              <div slot="error" class="image-slot">
                <img src="@/assets/images/error.gif" alt="" width="200px" height="150px">
              </div>
            </el-image>
          </template>
        </el-table-column>
        <el-table-column label="标题" prop="title" width="400px"></el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status===0" >草稿</el-tag>
            <el-tag v-if="scope.row.status===1" type="info">待审核</el-tag>
            <el-tag v-if="scope.row.status===2" type="success">审核通过</el-tag>
            <el-tag v-if="scope.row.status===3" type="warning">审核失败</el-tag>
            <el-tag v-if="scope.row.status===4" type="danger">已删除</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="发布时间" prop="pubdate"></el-table-column>
        <el-table-column label="操作" width="120px">
          <template slot-scope="scope">
            <el-button @click="editArticles(scope.row.id)" type="primary" icon="el-icon-edit" circle plain></el-button>
            <el-button @click="deleteArticles(scope.row.id)" type="danger" icon="el-icon-delete" circle plain></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        style="margin-top:20px"
        background
        layout="prev, pager, next"
        @current-change="togglePage"
        :current-page="reqParams.page"
        :page-size="reqParams.per_page"
        :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      reqParams: {
        // 默认为null ，使用axios 是不会给服务端传递的
        status: null,
        // 频道参数
        channel_id: null,
        // 时间参数
        begin_pubdate: null,
        end_pubdate: null,
        // 当前页
        page: 1,
        // 没页显示的条数
        per_page: 20
      },
      // 频道
      channelOptions: [],
      // 时间数组
      dataArr: [],
      // 文章列表
      articleTable: [],
      // 总条数
      total: 0
    }
  },
  created () {
    this.getArticleTable()
  },
  methods: {
    // 获取表格
    async getArticleTable () {
      const { data: { data } } = await this.$axios.get('/mp/v1_0/articles', { params: this.reqParams })
      // console.log(data);
      this.articleTable = data.results
      this.total = data.total_count
    },
    // 切换分页  默认传入 改变后的 页数
    togglePage (changePage) {
      // console.log(changePage);
      this.reqParams.page = changePage
      // 再次发送请求
      this.getArticleTable()
    },
    // 筛选表格,这里完成了频道和状态的筛选
    filterChannel () {
      // 需要把页码变成第一页
      this.reqParams.page = 1
      this.getArticleTable()
    },
    // 筛选时间
    changeDate (changedDate) {
      // console.log(changedDate);
      if (changedDate) {
        // console.log(changedDate[0]);
        this.reqParams.begin_pubdate = changedDate[0]
        this.reqParams.end_pubdate = changedDate[1]
      } else {
        this.reqParams.begin_pubdate = null
        this.reqParams.end_pubdate = null
      }
    },
    // 编辑跳转
    editArticles (id) {
      this.$router.push({ path: '/publishedArticle', query: { id } })
    },
    // 删除文章
    deleteArticles (id) {
      this.$confirm('此操作将永久删除该文章, 是否继续?', '温馨提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await this.$axios.delete(`/mp/v1_0/articles/${id}`)
        // 再次发送请求
        this.getArticleTable()
        this.$message.success('删除成功！')
      }).catch(() => {
        // 取消不做操作
      })
    }
  }
}
</script>

<style>

</style>
